home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / ingres04.lzh / source / dbu / convert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-01-23  |  2.7 KB  |  136 lines

  1. # include    <ingres.h>
  2. # include    <symbol.h>
  3. # include    <sccs.h>
  4.  
  5. SCCSID(@(#)convert.c    8.2    1/16/85)
  6.  
  7. /*
  8. **    convert  converts  numeric values of one type and length
  9. **    to a different type and length.
  10. **
  11. **    The source numeric can be i1,i2,i4,f4,or f8.
  12. **    The source number will be converted to the
  13. **    type and length specified in the destination.
  14. **    It also  must be one of i1,i2,i4,f4, or f8.
  15. **
  16. **    convert returns 0 is no overflow occured,
  17. **      else it returns -1
  18. */
  19.  
  20. convert(inp, outp, sf, slen, df, dlen)
  21. char    *inp;    /* pointer to input */
  22. char    *outp;    /* pointer to the output area */
  23. int    sf;    /* format of the source number */
  24. int    slen;    /* length of the source number */
  25. int    df;    /* format of the dest */
  26. int    dlen;    /* length of the dest */
  27. {
  28.     union anytype    number;
  29.     register union anytype    *num;
  30.     register int    sl;
  31.     register int    dl;
  32.  
  33.     dl = dlen;
  34.     sl = slen;
  35.     num = &number;
  36.     bmove(inp, num, sl);    /* copy number into buffer */
  37.  
  38.     if (sf != df)
  39.     {
  40.         /* if the source and destination formats are
  41.            different then the source must be converted
  42.            to i4 if the dest is int, otherwise to f8 */
  43.  
  44.         if (df == FLOAT)
  45.         {
  46.             switch (sl)
  47.             {
  48.  
  49.               case 1:
  50.                 num->f8type = num->i1type;    /* i1 to f8 */
  51.                 break;
  52.  
  53.               case 2:
  54.                 num->f8type = num->i2type;    /* i2 to f8 */
  55.                 break;
  56.  
  57.               case 4:
  58.                 num->f8type = num->i4type;    /* i4 to f8 */
  59.             }
  60.         sl = 8;
  61.         }
  62.         else
  63.         {
  64.             /* check if float >  2**31 */
  65.             if (sl == 8)
  66.                 num->f4type = num->f8type;    /* f8 to f4 */
  67.  
  68.             if (num->f4type > MAXF4 | num->f4type < MINF4 )
  69.                 return (-1);
  70.             num->i4type = num->f4type;    /* f4 to i4 */
  71.             sl = 4;
  72.         }
  73.     }
  74.  
  75.     /* source is now the same type as destination */
  76.     /* convert lengths to match */
  77.  
  78.     if (sl != dl)
  79.     {
  80.         /* lengths don't match. convert. */
  81.         if (df == FLOAT)
  82.         {
  83.             if (dl == 8)
  84.                 num->f8type = num->f4type;    /* f4 to f8 */
  85.             else
  86.                 num->f4type = num->f8type;    /* f8 to f4 with rounding */
  87.         }
  88.         else
  89.         {
  90.             switch (dl)
  91.             {
  92.  
  93.               case 1:
  94.                 if (sl == 2)
  95.                 {
  96.                     if (num->i2type > MAXI2 | num->i2type < MINI2)
  97.                         return (-1);
  98.                     num->i1type = num->i2type;    /* i2 to i1 */
  99.                 }
  100.                 else
  101.                 {
  102.                     if (num->i4type > MAXI2 | num->i4type < MINI2)
  103.                         return (-1);
  104.                     num->i1type = num->i4type;    /* i4 to i1 */
  105.                 }
  106.                 break;
  107.  
  108.               case 2:
  109.                 if (sl == 1)
  110.                 {
  111.                     num->i2type = num->i1type;    /* i1 to i2 */
  112.                 }
  113.                 else
  114.                 {
  115.                     if (num->i4type > MAXI4 | num->i4type < MINI4)
  116.                         return (-1);
  117.                     num->i2type = num->i4type;    /* i4 to i2 */
  118.                 }
  119.                 break;
  120.  
  121.               case 4:
  122.                 if (sl == 1)
  123.                     num->i4type = num->i1type;    /* i1 to i4 */
  124.                 else
  125.                     num->i4type = num->i2type;    /* i2 to i4 */
  126.             }
  127.         }
  128.     }
  129.  
  130.     /* conversion is complete */
  131.     /* copy the result into outp */
  132.  
  133.     bmove(num, outp, dl);
  134.     return (0);
  135. }
  136.